//
//  ZBShareMenuView.m
//  Investment
//
//  Created by xujiuming on 14-12-7.
//  Copyright (c) 2014年 xujiuming. All rights reserved.
//

#import "MessagePhotoViewMore.h"
#import "ZYQAssetPickerController.h"
// 每行有4个
#define kZBMessageShareMenuPerRowItemCount 4
#define kZBMessageShareMenuPerColum 2

#define kZBShareMenuItemIconSize 60
#define KZBShareMenuItemHeight 80

#define MaxItemCount 12
#define ItemWidth 50
#define ItemHeight 50


@interface MessagePhotoViewMore (){
    //UILabel *lblNum;
}


/**
 *  这是背景滚动视图
 */
@property(nonatomic,strong) UIScrollView *photoScrollView;
@property (nonatomic, weak) UIScrollView *shareMenuScrollView;
@property (nonatomic, weak) UIPageControl *shareMenuPageControl;
@property(nonatomic,weak)UIButton *btnviewphoto;
@end

@implementation MessagePhotoViewMore
@synthesize photoMenuItems;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)photoItemButtonClicked:(UIButton *)sender {
    /*
    if ([self.delegate respondsToSelector:@selector(didSelecteShareMenuItem:atIndex:)]) {
        NSInteger index = sender.tag;
        //NSLog(@"self.photoMenuItems.count is %d",self.photoMenuItems.count);
        if (index < self.photoMenuItems.count) {
            [self.delegate didSelectePhotoMenuItem:[self.photoMenuItems objectAtIndex:index] atIndex:index];
        }
    }
    */
}

- (void)setup{
   
    self.backgroundColor = [UIColor colorWithRed:248.0f/255 green:248.0f/255 blue:255.0f/255 alpha:1.0];

    _photoScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 105)];
    //_photoScrollView.contentSize = CGSizeMake(1024, 60);
    //_photoScrollView.alwaysBounceHorizontal = YES;
   
    photoMenuItems = [[NSMutableArray alloc]init];
    _itemArray = [[NSMutableArray alloc]init];
    [self addSubview:_photoScrollView];
    //lblNum = [[UILabel alloc]initWithFrame:CGRectMake(50, 170, 230, 30)];
    //[self addSubview:lblNum];
    
    [self initlizerScrollView:self.photoMenuItems];

}

-(void)reloadDataWithImage:(UIImage *)image{
    [self.photoMenuItems addObject:image];
    
    [self initlizerScrollView:self.photoMenuItems];
}

-(void)initlizerScrollView:(NSArray *)imgList{
    [self.photoScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    int iColumn=0;
    int iRow=0;
    for(int i=0;i<imgList.count;i++){
        //ALAsset *asset=imgList[i];
        //UIImage *tempImg=[UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
        
        UIImage *tempImg = [imgList objectAtIndex:i];
        iColumn = i%6;
        iRow = i/6;
        //删除按钮
        MessagePhotoMenuItem *photoItem = [[MessagePhotoMenuItem alloc]initWithFrame:CGRectMake(5+iColumn * (ItemWidth + 5 ), 5+ItemHeight*iRow, ItemWidth,ItemHeight)];
        //屏蔽，单行使用 MessagePhotoMenuItem *photoItem = [[MessagePhotoMenuItem alloc]initWithFrame:CGRectMake(i * (ItemWidth + 5 ), 5, ItemWidth, ItemHeight)];
        //删除==MessagePhotoMenuItem *photoItem = [[MessagePhotoMenuItem alloc]initWithFrame:CGRectMake(5 + (ItemWidth + 3) * imgList.count, 5, 50, 50)];
        photoItem.delegate = self;
        photoItem.index = i;
        photoItem.contentImage = tempImg;
        [self.photoScrollView addSubview:photoItem];
        [self.itemArray addObject:photoItem];
        
        //self.photoScrollView.contentSize=CGSizeMake((i+1)*(ItemWidth + 5), 60);
    }
    if(imgList.count<MaxItemCount){
        iColumn = imgList.count%6;
        iRow = (int)imgList.count/6;
        
        UIButton *btnphoto=[UIButton buttonWithType:UIButtonTypeCustom];
        //[btnphoto setFrame:CGRectMake(5 + (ItemWidth + 3) * imgList.count, 5, 50, 50)];
        [btnphoto setFrame:CGRectMake(5+(ItemWidth + 5) * iColumn, 5 + ItemHeight*iRow, 50, 50)];
        //[btnphoto setFrame:CGRectMake(5+(ItemWidth + 5) * imgList.count, 5 + ItemHeight*iRow, 50, 50)];
        [btnphoto setImage:[UIImage imageNamed:@"icon02.png"] forState:UIControlStateNormal];
        [btnphoto setImage:[UIImage imageNamed:@"icon02.png"] forState:UIControlStateSelected];
        //给添加按钮加点击事件
        [btnphoto addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside];
        [self.photoScrollView addSubview:btnphoto];
    }
    
    //NSInteger count = MIN(imgList.count +1, MaxItemCount);
    //lblNum.text = [NSString stringWithFormat:@"已选%d张，共可选10张",self.photoMenuItems.count];
    //lblNum.backgroundColor = [UIColor clearColor];
    //NSLog(@"%i------%li",(5 + ItemWidth + 5)*count,(long)count);
    //[self.photoScrollView setContentSize:CGSizeMake((5 + ItemWidth)*count, 0)];
    //[self.photoScrollView setContentSize:CGSizeMake((5 + ItemWidth)*iColumn, 5 + ItemHeight*(iRow+1))];
}
-(void)openMenu{
    //在这里呼出下方菜单按钮项
    myActionSheet = [[UIActionSheet alloc]
                     initWithTitle:nil
                     delegate:self
                     cancelButtonTitle:@"取消"
                     destructiveButtonTitle:nil
                     otherButtonTitles:@"拍照",@"从手机相册选择", nil];
    
    [myActionSheet showInView:self.window];
}
//下拉菜单的点击响应事件
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if(buttonIndex == myActionSheet.cancelButtonIndex){
        NSLog(@"取消");
    }
    //导航栏颜色保持和当前一致
    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName]];
    switch (buttonIndex) {
        case 0:
            [self takePhoto];
            break;
        case 1:
            [self localPhoto];
            break;
        default:
            break;
    }
}

//开始拍照
-(void)takePhoto{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [self.delegate addUIImagePicker:picker];
    
        
    }else{
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
}

/*
    新加的另外的方法
 */
////////////////////////////////////////////////////////////
//打开相册，可以多选
-(void)localPhoto{
    ZYQAssetPickerController *picker = [[ZYQAssetPickerController alloc]init];
    
    picker.maximumNumberOfSelection = 12;
    picker.assetsFilter = [ALAssetsFilter allPhotos];
    picker.showEmptyGroups = NO;
    picker.delegate = self;
    picker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject,NSDictionary *bindings){
        if ([[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyType]isEqual:ALAssetTypeVideo]) {
            NSTimeInterval duration = [[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyDuration]doubleValue];
            return duration >= 10;
        }else{
            return  YES;
        }
    }];
    
    [self.delegate addPicker:picker];
}

#pragma  mark   -ZYQAssetPickerController Delegate

/*
 得到选中的图片
 */
#pragma mark - ZYQAssetPickerController Delegate
//跳转到大图显示页面
-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{
    
    //[self.scrollview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
   
    //NSLog(@"self.itemArray is %lu",(unsigned long)self.photoMenuItems.count);
    //    NSLog(@"assets is %d",assets.count);
        //跳转到显示大图的页面
        //ShowBigViewController *big = [[ShowBigViewController alloc]init];
      
        //big.arrayOK = [NSMutableArray arrayWithArray:assets];
    
    //追加状态
    for (int i=0; i<assets.count; i++) {
        ALAsset *asset=assets[i];
        UIImage *tempImg=[UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
        [self reloadDataWithImage:tempImg];
    }

    
//    if (self.photoMenuItems.count>0) {
//        [self.photoMenuItems addObjectsFromArray:[NSMutableArray arrayWithArray:assets]];
//    } else
//        self.photoMenuItems = [NSMutableArray arrayWithArray:assets];
//
//    [self initlizerScrollView:self.photoMenuItems];
        //NSLog(@"arraryOk is %d",big.arrayOK.count);
        //[picker pushViewController:big animated:YES];
    [self.delegate selectItemArray:[NSMutableArray arrayWithArray:assets]];
  
}
/////////////////////////////////////////////////////////

//选择某张照片之后
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    //关闭相册界面
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    //当选择的类型是图片
    if([type isEqualToString:@"public.image"]){
        //先把图片转成NSData
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [self reloadDataWithImage:image];
        
        [self.delegate takePhoto:image];
        //NSData *datas=UIImageJPEGRepresentation(image, 0.5);
       // [datas writeToFile:self.imagepath atomically:YES];
//        if(UIImagePNGRepresentation(image)==nil){
//            datas = UIImageJPEGRepresentation(image, 1.0);
//        }else{
//            datas = UIImagePNGRepresentation(image);
//        }
        /*
        //图片保存的路径
        //这里将图片放在沙盒的documents文件夹中
        NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        //文件管理器
        NSFileManager *fileManager = [NSFileManager defaultManager];
        //把刚才图片转换的data对象拷贝至沙盒中,并保存为image.png
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:datas attributes:nil];
        //得到选择后沙盒中图片的完整路径
        filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,@"/image.png"];
        */
        //创建一个选择后图片的图片放在scrollview中
        
        //加载scrollview中
        
    }
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    NSLog(@"您取消了选择图片");
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)reloadData {
    
}
- (void)dealloc {
    //self.shareMenuItems = nil;
    self.photoScrollView.delegate = self;
    self.shareMenuScrollView.delegate = self;
    self.shareMenuScrollView = nil;
    self.shareMenuPageControl = nil;
}

#pragma mark - MessagePhotoItemDelegate

-(void)messagePhotoItemView:(MessagePhotoMenuItem *)messagePhotoItemView didSelectDeleteButtonAtIndex:(NSInteger)index{
    [self.photoMenuItems removeObjectAtIndex:index];
    [self initlizerScrollView:self.photoMenuItems];
    [self.delegate deleteImage:index];
}

#pragma mark - UIScrollView delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //每页宽度
    CGFloat pageWidth = scrollView.frame.size.width;
    //根据当前的坐标与页宽计算当前页码
    NSInteger currentPage = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth)+1;
    [self.shareMenuPageControl setCurrentPage:currentPage];
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
